package com.appkudos.view;

import java.text.DecimalFormat;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.appkudos.view.R;
import android.widget.Spinner;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import com.appkudos.controller.RecController;
import com.appkudos.controller.UserController;
import com.appkudos.model.Group;
import com.appkudos.model.User;

/**
 * This class is deprecated after switch to using views vs. activities for tab content.
 * 
 * @author svandy624
 *
 */
public class WriteReviewActivity extends Activity {
	
	private Activity PARENTACTIVITY; 
	Bundle extras;
	Spinner recToFriendSpinner;
	Spinner recToGroupSpinner;
	TextView appText;
	EditText commentEditText;
	Button postReviewBtn;
	String appName;
	String appDesc;
	String appId;
	
	UserController uc = new UserController();
	RecController rc =  new RecController();
	Group group;
	User user;
	App app;
	List<Group> user_groups;
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	PARENTACTIVITY = this;
        setContentView(R.layout.view_write_review);
        
        // get data from bundle
    	Intent launchIntent = getIntent();
	    extras = launchIntent.getExtras();
        uc = new UserController();
        group = uc.getCurrentGroup(extras);
        user = uc.getCurrentUser(extras);
        appName = extras.getString("app_name");
        appDesc = extras.getString("app_desc");
        appId = extras.getString("app_id");
        
        initWidgets();
        populateRecommendTo();
    }

    /**
     * Initialize components for Write Review screen.
     */
    public void initWidgets() {
    	recToFriendSpinner = (Spinner) findViewById(R.id.wr_rec_to_friend_spinner);
    	recToGroupSpinner = (Spinner) findViewById(R.id.wr_rec_to_group_spinner);
    	
    	appText = (TextView) findViewById(R.id.app_text);
    	
    	// fill app name based on previous screen
    	if (extras!= null) {
    		appText.setText(extras.getString("app_name"));
    	}
    	
    	appText.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {     
            	// hook button to Write Review screen
            	Intent intent = new Intent(PARENTACTIVITY, App.class);
        	    intent = uc.passCurrentUser(intent, user);
        	    intent.putExtra("app_name", appName);
        	    intent.putExtra("app_desc", appDesc);
        	    startActivity(intent);
        	    
            }
          });  
    	commentEditText = (EditText) findViewById(R.id.wr_comment_edittext);
    	
    	postReviewBtn = (Button) findViewById(R.id.wr_post_review_btn);
    	postReviewBtn.setOnClickListener(myListener);
    }
    public void populateRecommendTo() {
    	
    	//Friends
    	//UserController uc = new UserController();
    	List<User> user_friends = uc.getFriendsOfUser(user.getUsername());
    	
    	ArrayAdapter <CharSequence> adapter =
    		  new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item);
    	adapter.add("Choose a friend");
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	for (int i=0; i<user_friends.size(); i++){
    		adapter.add(user_friends.get(i).getUsername());
    	}
    	
    	recToFriendSpinner.setAdapter(adapter); 		 
    	
    	//Groups
    	user_groups = uc.getGroupsOfUser(user.getUsername());
    	
    	ArrayAdapter <CharSequence> adapter2 =
    		  new ArrayAdapter <CharSequence> (this, android.R.layout.simple_spinner_item);
    	adapter2.add("Choose a Group");
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	for (int i=0; i<user_groups.size(); i++){
    		adapter2.add(user_groups.get(i).getGroupName());
    	}
    	
    	// if group has been selected for an app to be recommended to it, fill in group name in spinner
    	if (group != null) {
    		String groupName = group.getGroupName();
    		Toast.makeText(getApplicationContext(), "Group " + groupName + " was selected",Toast.LENGTH_SHORT).show();
    		// search for this group name in the spinner
    		int spinnerPosition = adapter.getPosition(groupName);
    		Toast.makeText(getApplicationContext(), "Group " + spinnerPosition + " was selected",Toast.LENGTH_SHORT).show();
    		// set the spinner to display this group name
    		recToGroupSpinner.setAdapter(adapter2);
    		recToGroupSpinner.setSelection(spinnerPosition);
    	}
    	else {
    		recToGroupSpinner.setAdapter(adapter2);
    	}
    }
    
    private OnClickListener myListener = new OnClickListener() {
    	public void onClick(View v){
    		int friend = recToFriendSpinner.getSelectedItemPosition();
    		int group = recToGroupSpinner.getSelectedItemPosition();
    		String comment = commentEditText.getText().toString();
    		
    		if (friend == 0 && group == 0){
    			Toast.makeText(getApplicationContext(), "No friend/group selected",Toast.LENGTH_SHORT).show();
    		}else{
    			//TODO (The value of username and appid has to be changed)"
    			if (friend != 0){
    				String friendString = recToFriendSpinner.getItemAtPosition(friend).toString();
    				rc.addRecommendation(user.getUsername(), comment, appName, 
        					friendString, false);
    			}
    			
    			if (group != 0){
    				String groupString = recToGroupSpinner.getItemAtPosition(group).toString();
    				rc.addRecommendation(user.getUsername(), comment, appName, 
        					groupString, true);
    			}
    			
    			Toast.makeText(getApplicationContext(), "Rec done!",Toast.LENGTH_SHORT).show();
    			Intent intent = new Intent(PARENTACTIVITY, com.appkudos.view.App.class);
	        	intent = uc.passCurrentUser(intent, user);
	        	intent.putExtra("app_name", appName);
	        	startActivity(intent);
    		}
    		
    		//showDialog( 0 );
        }
    };
}
